package com.company.Hard;

import java.util.*;

public class LK123 {
    public static void main(String[] args) {
        int[] arr = {4,5,6,0,0,9};
        System.out.println(add(arr,0));
    }
    public static long run(int n){
        long x = 0;
        int flag = n/7;
        int temp = n%7;
        for(int i =0;i<flag;i++){
            x += 2900 + 700*i;
        }
        for(int i =0;i<temp;i++){
            x += 100 + flag*100 +i*100;
        }
        return x;
    }
    public static  int add(int[] num,int target){
        int i = 0;
        int j = num.length-1;
        Arrays.sort(num);
        int min = Integer.MAX_VALUE;
        int z = 0;
        while(i<j){
            int temp = num[i]+num[j];
            if(temp>target){
                j--;
            }else if(temp<target){
                i++;
            }else{
                return temp;
            }
            if(Math.abs(temp-target)<min&&temp<target){
                min = Math.abs(temp-target);
                z = temp;
            }
        }
        return z==0?-1:z;
    }
    public static int maxProfit(int[] prices) {
        List<Integer> list = new ArrayList<>();
        int i = 1;
        int len = prices.length;
        int min = prices[0];
        while(i<len){
            while(i<len&&prices[i]<=prices[i-1]){
                i++;
            }
            int temp = prices[i-1];
            while(i<len&&prices[i]>=prices[i-1]){
                i++;
            }
            list.add(prices[i-1]-temp);
        }
        list.sort((Integer a,Integer b)->{
            return b-a;
        });

        return list.size()>=2?list.get(0)+list.get(1):list.get(0);
    }
}
